import count from '@dfeidao/atom-nodejs/db/count';
import query from '@dfeidao/atom-nodejs/db/query';
// import { systemid } from './config';

interface Message {
	productid: string;
	role_no: string;
	search_field_val: object;
	page_size: number;
	page_no: number;
}

export default async function atom(msg: Message, action_id: string, session_id: string, spaceid: string) {
	const systemid = '12';

	const { exec, prepare } = query(action_id, session_id, systemid);
	const productid = msg.productid;
	const role_no = msg.role_no;
	const search_field_val = msg.search_field_val;
	const filter = {
		productid, role_no
	} as {
		search_field_val: {};
		productid: string;
		role_no: string;
	};
	if (search_field_val) {
		filter.search_field_val = { $like: `%${search_field_val}%` };
	}
	prepare('pub_role_allot', ['pk_val', 'search_field_val', 'object_type_id'], filter, msg.page_size, msg.page_no, [], []);
	const [data] = await exec();
	const c = count(action_id, session_id, systemid);
	c.prepare('pub_role_allot', { productid, role_no, search_field_val });
	const [t] = await c.exec();
	return { dataArray: data, count: t };
}
